//
//  SegmentedView.m
//  项目PianKe
//
//  Created by 关雷 on 14-12-29.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SegmentedView.h"
#import "ReadViewController.h"
@implementation SegmentedView

- (void)dealloc
{
    [_segV1 release];
    [_segV2 release];
    [_segV3 release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame imgNames:(NSArray *)imgNames  title1:(NSString *)title1 title2:(NSString *)title2 title3:(NSString *)title3
{
    self = [super initWithFrame:frame];
    if (self) {
        self.segV1 = [[MySegView alloc] initWithFrame:CGRectMake(frame.size.width / 6 - frame.size.height / 2, frame.size.height / 4, frame.size.height + 15, frame.size.height / 2) imgName:[imgNames objectAtIndex:0] title:title1];
        _segV1.imgV.image = [_segV1.imgV.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [self addSubview:_segV1];
        [_segV1 release];
        
        self.segV2 = [[MySegView alloc] initWithFrame:CGRectMake(frame.size.width / 2 - frame.size.height / 2, frame.size.height / 4, frame.size.height + 15, frame.size.height / 2) imgName:[imgNames objectAtIndex:1] title:title2];
        [self addSubview:_segV2];
        [_segV2 release];
        
        self.segV3 = [[MySegView alloc] initWithFrame:CGRectMake(frame.size.width / 6 * 5 - frame.size.height / 2, frame.size.height / 4, frame.size.height + 15, frame.size.height / 2) imgName:[imgNames objectAtIndex:2] title:title3];
        [self addSubview:_segV3];
        [_segV3 release];
        
           }
    return self;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
     NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    _segV1.lable.highlighted = NO;
    _segV2.lable.highlighted = NO;
    _segV3.lable.highlighted = NO;
    UITouch * touch = [touches anyObject];
    self.point = [touch locationInView:self.superview];
    if (self.point.x < self.frame.size.width / 3) {
        _segV1.lable.highlighted = YES;
        if ([(NSObject *)self.target respondsToSelector:self.action]) {
            [(NSObject *)self.target performSelector:self.action withObject:self];
        }
        [defaults setObject:@"1" forKey:@"picNum"];
        [defaults synchronize];
        
    } else if (self.point.x > self.frame.size.width / 3 * 2) {
        _segV3.lable.highlighted = YES;
        if ([(NSObject *)self.target respondsToSelector:self.action2]) {
            [(NSObject *)self.target performSelector:self.action2 withObject:self];
        }
        [defaults setObject:@"3" forKey:@"picNum"];
        [defaults synchronize];
        
    } else {
        _segV2.lable.highlighted = YES;
        if ([(NSObject *)self.target respondsToSelector:self.action1]) {
            [(NSObject *)self.target performSelector:self.action1 withObject:self];
        }
        [defaults setObject:@"2" forKey:@"picNum"];
        [defaults synchronize];
    }
    
    NSString * pic = [defaults objectForKey:@"picNum"];
    if ([pic isEqualToString:@"1"]) {
        _segV1.imgV.image = [_segV1.imgV.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    } else {
        _segV1.imgV.image = [_segV1.imgV.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    if ([pic isEqualToString:@"2"]) {
        _segV2.imgV.image = [_segV2.imgV.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    } else {
        _segV2.imgV.image = [_segV2.imgV.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    if ([pic isEqualToString:@"3"]) {
        _segV3.imgV.image = [_segV3.imgV.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    } else {
        _segV3.imgV.image = [_segV3.imgV.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
}




@end
